package com.rxyl.love.petdoctor.adapter;

import android.content.Context;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.rxyl.love.petdoctor.R;
import com.rxyl.love.petdoctor.app.MyApplication;
import com.rxyl.love.petdoctor.entity.CircleEntity;
import com.rxyl.love.petdoctor.view.MyGridView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zuchang on 2016/1/12.
 */
public class CircleItemAdapter<T> extends BaseAdapter{

    private Context context;
    private List<T> objects;
    private LayoutInflater layoutInflater;

    public CircleItemAdapter(Context context, List<T> objects){
        this.objects = objects;
        this.context = context;
    }




    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public Object getItem(int position) {
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        CircleEntity.Items entity = (CircleEntity.Items) objects.get(position);
        ViewHolder vh;
        if(convertView==null){
           convertView = View.inflate(context,R.layout.circle_user,null);
            vh = new ViewHolder();
            vh.iv_userportrait = (SimpleDraweeView) convertView.findViewById(R.id.iv_userportrait);
            vh.tv_username = (TextView) convertView.findViewById(R.id.tv_user_name);
            vh.tv_passtime = (TextView) convertView.findViewById(R.id.tv_pass_time);
            vh.tv_usertalk = (TextView) convertView.findViewById(R.id.tv_user_talk);
            vh.gv_userphotos = (MyGridView) convertView.findViewById(R.id.gv_user_talk_img);
            vh.tv_likes_count = (TextView) convertView.findViewById(R.id.talk_likes_count);
            vh.imageView_zan = (ImageView) convertView.findViewById(R.id.im_zan);
            vh.tv_talk_gander = (TextView) convertView.findViewById(R.id.talk_gander);
            convertView.setTag(vh);
        }else{
            vh = (ViewHolder) convertView.getTag();
        }
        vh.tv_username.setText(entity.getCreator().getName());
        vh.tv_usertalk.setText(entity.getContent());
        vh.tv_passtime.setText(entity.getCreate_time());
        vh.tv_likes_count.setText("   "+entity.getLikes_count());
        vh.tv_talk_gander.setText("   "+entity.getCreator().getGender());

        //判断是否已赞
        if (entity.isLiked()){
            vh.imageView_zan.setImageResource(R.mipmap.umeng_comm_like_pressed);
        }

        String icon_uri = entity.getCreator().getIcon_url().getOrigin();
     //   MyApplication.getApp().getImageLoader().displayImage(icon_uri,vh.iv_userportrait,MyApplication.getApp().getImageOptions());

        if(icon_uri!=null){
            vh.iv_userportrait.setImageURI(Uri.parse(icon_uri));
        }else{
            vh.iv_userportrait.setImageURI(Uri.parse(""));
        }

        int len =  entity.getImage_urls().size();
        List<String> pic = new ArrayList<>();

        for (int i = 0; i <len ; i++) {
            pic.add(entity.getImage_urls().get(i).getI360());
            Log.d("tagg", "getView: "+entity.getImage_urls().get(i).getOrigin());
        }
        vh.gv_userphotos.setAdapter(new CircleGridViewAdapter<String>(context,pic));
        return convertView;
    }
    class ViewHolder {
        private SimpleDraweeView iv_userportrait;
        private TextView tv_username,tv_passtime,tv_usertalk,tv_likes_count,tv_talk_gander;
        private MyGridView gv_userphotos;
        private ImageView imageView_zan;
    }

    /**
     * 说说图片适配器
      * @param <T>
     */

    class CircleGridViewAdapter<T> extends BaseAdapter{
        private List<String> photos ;
        private Context context;

        public CircleGridViewAdapter( Context context,List<String> photos){
            this.context = context;
            this.photos = photos;
        }

        @Override
        public int getCount() {
            return photos.size();
        }

        @Override
        public Object getItem(int position) {
            return photos.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = View.inflate(context,R.layout.image_circletalk,null);
            //动态设置图片大小
            convertView.setLayoutParams(new GridView.LayoutParams(240, 240));


            String str =  photos.get(position);
            Log.d("tagg", "getView: "+str);
            ImageView iv = (ImageView) convertView.findViewById(R.id.circle_imgs_talk);
            MyApplication.getApp().getImageLoader().displayImage(str,iv,MyApplication.getApp().getImageOptions());
           return convertView;
        }
    }

}
